package com.ihr360.expr.function.excel;


import com.ihr360.expr.ExprArray;
import com.ihr360.expr.ExprDouble;
import com.ihr360.expr.ExprError;
import com.ihr360.expr.ExprException;
import com.ihr360.expr.ExprString;
import com.ihr360.expr.context.IEvaluationContext;
import com.ihr360.expr.core.AbstractFunction;
import com.ihr360.expr.core.Expr;
import com.ihr360.expr.utils.ExprUtils;

public class LEN extends AbstractFunction
{
    @Override
    public Expr evaluate(IEvaluationContext context, Expr[] args)
            throws ExprException {
        assertArgCount(args, 1);
        Expr a = ExprUtils.evalArg(context, args[0]);
        if (a instanceof ExprArray){
            return ExprError.VALUE;
        }
        String str = null;
        if (a instanceof ExprString){
            str = ((ExprString) a).str;
        }
        else{
            str = a.toString();
        }
        return new ExprDouble(str.length());
    }
}
